package com.rpc.consumer.invoker;

import com.rpc.common.entity.request.RPCDTO;
import com.rpc.consumer.client.RPCConsumerClient;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * 动态代理的调用处理器
 */
public class RPCConsumerInvokerHandler implements InvocationHandler {
    private String interfaceName;
    private RPCConsumerClient consumerClient = new RPCConsumerClient();

    public RPCConsumerInvokerHandler(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public RPCConsumerInvokerHandler(Class type) {
        interfaceName = type.getCanonicalName();
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RPCDTO rpcdto = new RPCDTO();
        rpcdto.setArgs(args);
        rpcdto.setClassName(interfaceName);
        rpcdto.setMethodName(method.getName());
        return consumerClient.doConsumer(rpcdto);   //消费
    }
}
